Once you're able to evaluate your RAG system and experiment with different configurations, you'll be ready to take on some familiar trade-offs in many software projects. Cost, speed, and quality. You'll explore these trade-offs over the next few videos, but first let's introduce an important concept, quantization. In short, quantization is compression for both LLMs and vectors generated by embedding models. Quantization replaces the model weights inside an LLM or the values of an embedding vector with a compressed, lower precision data type. This makes the models or vectors respectively smaller, cheaper, and faster to run, often without much sacrifice in retrieval relevance or response quality. Let's have a look to see how it works. A good analogy for quantization is image compression. This high-quality image uses 24 bits of data to represent the color at each pixel. The colors look great, but it does use quite a lot of data to store all that information. You can compress the image by using 12 or even 6 bits for each pixel. The compressed images are 1 half and 1 quarter of the size of the original, which is a significant memory saving. That said, the 12-bit image already doesn't look as nice, and in the 6-bit image, there are many visible color artifacts. Depending on where you're using the image, however, this drop in quality might be worth the substantial memory savings. Quantization takes the same approach to LLMs and embedding vectors, shrinking their size with some loss in quality as a trade-off. Let's start by looking at how it works for LLMs. The parameters in a typical language model use 16 bits of memory each. With modern models ranging between roughly a billion to a trillion parameters, these models are huge, requiring lots of memory to store them and powerful GPUs to run them. Quantized models compress those 16-bit parameters down to 8 or even 4-bit equivalents. This significantly reduces the GPU memory required to run the model at the cost of a little model performance and quality. Quantized embedding vectors work similarly. A fairly typical 768-dimensional vector will use 768 32-bit floating point numbers. That's 3 kilobytes of data for every vector in your knowledge base. Higher dimensional models can easily require many times that much. Once you're storing millions or even billions of these vectors, you've got huge amounts of vector data to deal with. These vectors need to be stored somewhere to use them, and especially if you want to use them in rapid vector search, they need to be loaded into expensive RAM. Integer quantization is a commonly used approach for shrinking these vectors. It replaces the 32-bit floating point numbers with a much smaller integer, for example, an 8-bit integer. This means that your vectors are now immediately one quarter of their original size, a massive space saving. These integer values are also remarkably easy to calculate. Here's how the process works. You find the minimum and maximum values that appear in each dimension for your vector data. This will define the range that values in that dimension will fall between. Then you divide that range into 256 equally sized sections. The amount of unique values you can make with 8 bits. Those sections are then numbered 0, 1, 2, 3, and so on up to 255. Each floating point number from your original vectors are then just assigned an integer value equal to whatever section it falls within. If you also store the minimum value and the width of each section, you have all the information you need to calculate an approximation of the original 32-bit float, but using only 8 bits of data. Despite using only a quarter of the data and a seemingly naive compression algorithm, 8-bit integer quantization performs remarkably well. For benchmarks like Recall at K, you might only see a drop of a few percentage points with 8-bit quantization. These quantized embedding vectors, meanwhile, allow for less data to be stored in your vector database while powering faster search as the calculations required have been simplified. Quantized LLMs also often have only minor drops in performance when measured using commonly referenced benchmarks. Meanwhile, they use less GPU memory and can generate text more rapidly. In other words, these are huge memory and performance gains for a small drop in quality. While 8-bit integer quantized vectors are commonly used, 1-bit or binary quantized vectors are also gaining popularity. This approach compresses the size of a vector by a factor of 32, from 32 bits per dimension to just 1 bit, which is a massive saving. At this level of compression, each value in your vector is either a 1 or a 0, and just tells you if that value at that dimension was a positive or a negative number. As you might imagine, at these extreme compression levels, performance can drop noticeably for embedding model-based retrieval. That said, 1-bit quantization results in significantly smaller and faster vector-based retrieval. It can also be paired with other techniques, for example, performing fast retrieval based on a 1-bit quantized embedding model, and then rescoring using the full original 32-bit vector. Another way to shrink the size of your vectors is to use a Matryoshka embedding model, based on the Russian word for nesting dolls. These vectors are designed so that you can choose to use only a subset of a vector's dimensions when doing things like comparing similarity. For example, if the full vector has 1000 dimensions, you could choose to use only the first 500 or the first 100 dimensions. To enable this behavior, Matryoshka embedding models have a special property. Their dimensions are sorted by how information-dense they are. Here, information means how much statistical variance you'd expect to see in that dimension when embedding large amounts of text. In a typical embedding model, every dimension would have roughly the same amount of variance or information. In a Matryoshka model, thanks to the training process used, earlier dimensions will have more variance, which means more information content. Later dimensions have less variance, meaning they provide relatively less information, and so you pay less of a penalty for excluding them. There's a few ways to use Matryoshka vectors. You could choose to only ever use the first 100 dimensions, saving space and leading to faster computations, while preserving the maximum possible amount of information. Or you could always perform initial retrieval using the first 100 dimensions, then pull the remaining 900, now using the full 1000 dimensions, out of slower, cheaper memory to essentially help rescore the initial set of documents you retrieved. The flexible properties of Matryoshka models make it best suited for dynamic environments where you may want to quickly switch from low to high fidelity vector representations. While these advanced techniques show what's possible at the cutting edge of quantization, the core takeaway is that you probably should experiment with using integer quantized LLMs and embedding models. Most LLM and embedding model providers will make 8 or 4-bit quantized models available alongside their base models. The space and cost savings they provide can be significant, and the quality reductions are quite small.